package org.speed.sentinel;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolKey;

public class BizServiceCircuitBreaker extends HystrixCommand<String> {
	
	private BizService bizService;
	

	protected BizServiceCircuitBreaker(BizService bizService) {
		super(
              Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("hystrix.command.http"))
                        .andCommandKey(HystrixCommandKey.Factory.asKey("hystrix.command.http"))
                        .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("hystrix.command.http"))
                        .andCommandPropertiesDefaults(
                                HystrixCommandProperties.Setter()
                                        .withCircuitBreakerRequestVolumeThreshold(2)
                                        .withCircuitBreakerSleepWindowInMilliseconds(30 * 1000)
                                        .withFallbackEnabled(true)
                                        //.withExecutionIsolationThreadInterruptOnTimeout(true)
                                        .withExecutionTimeoutInMilliseconds(2000)));
		
		this.bizService = bizService;
        
	}

	@Override
	protected String run() throws Exception {
		
		bizService.exec();
		
		return "ok";
	}

	@Override
	protected String getFallback() {
		
		System.out.println("getFallback");
		
		return "error";
	}
	
	

}
